import requests
import time
import hashlib

def make_md5(text):
    m = hashlib.md5() # 创建一个 md5加密工具
    m.update(str(text).encode(encoding='utf-8'))
    res = m.hexdigest()  # 生成加密后的内容
    return res

s = requests.session()

# 注册
timestamp = time.strftime('%H%d%M%S')
register_url = "http://114.132.200.42:3456/user/register"
data = {
    'username': timestamp,
    'password': '123456',
    'cpassword': '123456',
    'checkcode': '1234',
    'email': timestamp+'@qq.com'
}
res = s.post(url=register_url,json=data).json()
if res['code'] == 0:
    print('注册成功')
else:
    print("注册失败")

# 登录
login_url = "http://114.132.200.42:3456/user/login"
data = {
    "username": timestamp,
    "password": make_md5('123456'),
    "checkcode": "1234"
}
res = s.post(url=login_url,json=data).json()
if res['code'] == 0:
    print('登录成功')
else:
    print("登录失败")
get_userId = res['userId']
get_userToken = res['userToken']


# 创建任务
creattask_url = "http://114.132.200.42:3456/task/info"
data = {
    "title": "nidayea"
}
head = {
    "userId": get_userId,
    "userToken": get_userToken
}
res = s.post(url=creattask_url,headers=head,json=data).json()
if res['code'] == 0:
    print('创建任务成功')
else:
    print("创建任务失败")


# 查询任务
queryTask_url = "http://114.132.200.42:3456/task/info"
data = {
    "status": 0
}
res = s.get(url=queryTask_url,headers=head,params=data).json()
# print(res)
# res['TaskList'][0]['task_id']
if res['code'] == 0:
    print('查询任务成功')
else:
    print("查询任务失败")
get_task_id = res['TaskList'][0]['task_id']


# 完成任务
completeTask_url = "http://114.132.200.42:3456/task/info"
data = {
    "task_id": get_task_id
}
res = s.put(url=completeTask_url,headers=head,json=data).json()
if res['action'] == '完成任务':
    print('完成任务成功')
else:
    print("完成任务失败")

completeTask_url = "http://114.132.200.42:3456/task/info"
data = {
    "task_id": get_task_id
}
res = s.put(url=completeTask_url,headers=head,json=data).json()
if res['action'] == '取消完成':
    print('取消任务成功')
else:
    print("取消任务失败")

completeTask_url = "http://114.132.200.42:3456/task/info"
data = {
    "task_id": get_task_id
}
res = s.delete(url=completeTask_url,headers=head,json=data).json()
if res['code'] == 0:
    print('删除任务成功')
else:
    print("删除任务失败")

# 上传头像
completeTask_url = "http://114.132.200.42:3456/user/uploal_action"
data = [
    ('pic',('lk.jpg',open('kenan.jpg','rb')))
]
res = s.post(url=completeTask_url,headers=head,files=data).json()
if res['code'] == 0:
    print('上传头像成功')
else:
    print("上传头像失败")

